package com.lzw.okgoutils.http.otherutils;

import android.util.Log;

import com.lzw.okgoutils.BuildConfig;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;

import static android.R.attr.tag;

/**
 * Log工具类
 */
public class LogUtil {

    //通过项目自动编译生成的BuildConfig文件判断是否是true，里面有个DEBUG常量
    // 如果当前项目是debug模式编译的，这个常量的值就是true,否则是false
    private static boolean LOG_ON = BuildConfig.DEBUG;

    private static boolean SAVE_LOG = false;
    private static boolean USER_COMMON_TAG = false;
    private static final String COMMON_TAG = "APP_COMMON_TAG";
    private static final String ERROR_TAG = "EXCEPTION";
    public static final String SAVE_DIR = BaseConstant.ROOT_STORAGE_PATH;

    public static void setLogOn(boolean isLogOn){
        LOG_ON = isLogOn;
    }

    public static boolean isLogEnable() {
        return LOG_ON;
    }

    public static void openLog(){
        LOG_ON = true;
    }

    public static void saveLog(boolean flg){
        SAVE_LOG = flg;
    }

    public static void closeLog(){
        LOG_ON = false;
    }

    public static void v(String tag, String text) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            Log.v(tag, text);
            writeLog(text);
        }
    }

    public static void e(String tag, String text) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            Log.e(tag, text);
            writeLog(text);
        }
    }

    public static void d(String tag, String text) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            Log.d(tag, text);
            writeLog(text);
        }
    }

    public static void i(String tag, String text) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            Log.i(tag, text);
            writeLog(text);
        }
    }

    public static void i(String tag, int value) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            String transformsValue = DataTypeTransformUtils.intToString(value);
            LogUtil.i(tag, transformsValue);
        }
    }

    public static void w(String tag, String text) {
        if (LOG_ON) {
            if (tag == null || USER_COMMON_TAG) {
                tag = COMMON_TAG;
            }
            Log.w(tag, text);
            writeLog(text);
        }
    }

    /**
     * 自动捕获哪里出现的异常
     * @param e  exception
     */
    public static void printExceptions(Exception e) {
        if(e == null){
            LogUtil.e(ERROR_TAG,"Caught expection but is null");
            return;
        }
        StackTraceElement stacks = e.getStackTrace()[1];
        String information = "Exception Found!: " + stacks.getClassName()
                + " ---------->OnLine: " + stacks.getLineNumber() + " "
                + stacks.getMethodName() + ": " + e.getLocalizedMessage();
        LogUtil.e(ERROR_TAG,information);
    }


    private static void writeLog(String text){
        if (SAVE_LOG) {
            writeLogZH(SAVE_DIR + "testlog.data", DateUtil.getFormatDateTime() + " tag: " + tag +" ---> "+text);
        }
    }

    public static void writeLog(String fileName, String content) {
        if(content == null || fileName == null){
            return;
        }
        content = content + "\n";
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            long fileLength = randomFile.length();
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 中文log写入sd卡
     * @param filePathAndName 文件路径
     * @param content log内容
     */
    public static void writeLogZH(String filePathAndName, String content) {
        if (content == null || filePathAndName == null) {
            return;
        }
        content = content + "\n";
        try {
            File f = new File(filePathAndName);
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(
                    new FileOutputStream(f, true), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(content);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}